<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<#include "/include/head_common.ftl">
	<#include "/include/login_trace.ftl">
	<#assign permissionSet = fm.getPermission() />
	<#assign edit= fm.hasPermission("sql:fieldTransform:edit",permissionSet)/>
	<#assign view= fm.hasPermission("sql:fieldTransform:view",permissionSet)/>
	<#assign disabled = edit?string('','disabled')/>
<title>
	<#if edit>
		列转换器<#if fieldTransform.id??>修改<#else>添加</#if>
	<#else>
		列转换器查看
	</#if>
</title>
<meta name="decorator" content="default" />
<script type="text/javascript">
	$(document).ready(
			function() {
				closeTip();
				$("#value").focus();
				$("#inputForm")
						.validate(
								{
									submitHandler : function(form) {
										loading('正在提交，请稍等...');
										form.submit();
									},
									errorContainer : "#messageBox",
									errorPlacement : function(error, element) {
										$("#messageBox").text("输入有误，请先更正。");
										if (element.is(":checkbox")
												|| element.is(":radio")
												|| element.parent().is(
														".input-append")) {
											error.appendTo(element.parent()
													.parent());
										} else {
											error.insertAfter(element);
										}
									}
								});
			});
	function goback(){
    	$.cookie('goback','goback',{path: '/',expires: 1});
    	history.go(-1);
    }
	function formatJson(id) {
		var format = $('#' + id).val().replace(/(^\s*)|(\s*$)/g, "");
		if(format.length > 0){
			$.ajax({
				type: "POST",
				url: "${rootUrl}/sql/json",
				contentType: "application/json;charset=UTF-8",
				dataType: 'json',
				data: format,
				success: function (data) {
					if (!data.isJson) {
						top.$.jBox.tip("非json格式", "error", {
							persistent : true,
							opacity : 0,
							timeout: 500
						});
					}else {
						$('#' + id).val(data.text.replaceAll('\t','\t\t').replaceAll(" ","  "));
					}
				}
			});
		}
	}

    function changeTransform(selectId, textAreaId, textId) {
        var classname = $('#' + selectId).val().replace(/(^\s*)|(\s*$)/g, "");
        if (classname.length > 0) {
            $.ajax({
                type: "GET",
                url: "${rootUrl}/sql/converterParam?classname=" + classname,
                contentType: "application/json;charset=UTF-8",
                dataType: 'json',
                success: function (data) {
                    if (data.success) {
                        $('#' + textAreaId).val(data.exampleConfig);
                        var exampleConfigDesc = data.exampleConfigDesc +"";
                        exampleConfigDesc = exampleConfigDesc.replaceAll("\n", "<BR>")
                            .replaceAll("\t", "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;")
                            .replaceAll(" ", "&nbsp;&nbsp;");
                        $('#' + textId).html(exampleConfigDesc);
                    }else {
						$('#' + textAreaId).val('');
						$('#' + textId).html('');
					}
                }
            });
        }
    }
	function resetForm(){
		$("#inputForm")[0].reset();
		$("#showDesc").html('');
	}
</script>
</head>
<body>
	<ul class="nav nav-tabs">
		<#if fm.hasPermission("sql:define:view",permissionSet) >
			<li><a href="${rootUrl}/sql/define/list">SQL列表</a></li>
		</#if>
		<#if fm.hasPermission("sql:define:edit",permissionSet) >
			<li><a href="${rootUrl}/sql/define/edit">SQL添加</a></li>
		</#if>
		<#if fm.hasPermission("sql:parameter:view",permissionSet) >
			<li><a href="${rootUrl}/sql/parameter/list">SQL参数列表</a></li>
		</#if>
		<#if fm.hasPermission("sql:parameter:edit",permissionSet) >
			<li><a href="${rootUrl}/sql/parameter/edit">SQL参数添加</a></li>
		</#if>
		<#if view >
			<li><a href="${rootUrl}/sql/fieldTransform/list?defineKey=${fieldTransform.defineKey!''}">列转换器列表</a></li>
		</#if>
		<li class="active">
			<a href="${rootUrl}/sql/fieldTransform/edit?id=${fieldTransform.id!''}">
				<#if edit>
					列转换器<#if fieldTransform.id??>修改<#else>添加</#if>
				<#else>
					列转换器查看
				</#if>
			</a>
		</li>
	</ul>
	<br />
	<form id="inputForm" action="${rootUrl}/sql/fieldTransform/edit" method="post"
		class="form-horizontal form-signin">
		<input type="hidden" name="id" value="${fieldTransform.id!''}" />
		<div class="control-group">
			<label class="control-label">定义的key:</label>
			<div class="controls">
				<select id="defineKey" name="defineKey" class="input-xxlarge required" <#if !edit || fieldTransform.id??> disabled </#if>>
					<option value="">请选择...</option>
					<#list defineList as define>
						<option value="${define.defineKey}"  <#if fieldTransform.defineKey?? && define.defineKey = fieldTransform.defineKey> selected </#if> >
							${define.defineKey}(${define.sqlDescribe})
						</option>
					</#list>
				</select>
			</div>
		</div>


		<div class="control-group">
			<label class="control-label">列名:</label>
			<div class="controls">
				<input name="fieldName" value="${fieldTransform.fieldName!''}" maxlength="50"
					   class="required"  type="text" ${disabled}/>
			</div>
		</div>

		<div class="control-group">
			<label class="control-label">列转换器:</label>
			<div class="controls">
				<select id="transformSelect" name="transform" class="input-xxlarge required" ${disabled} style="font-size: 15px; width: 800px;" onchange="changeTransform(this.id,'transformConstructArg','showDesc')">
					<option value="">请选择...</option>
					<#list classnames as classname>
						<option value="${classname}"  <#if fieldTransform.transform?? && fieldTransform.transform = classname> selected </#if> >${classname}</option>
					</#list>
				</select>
			</div>
		</div>

		<div class="control-group">
			<label class="control-label">构造参数:</label>
			<div class="controls">
                <textarea id = "transformConstructArg" name="constructArg" class="input-xxlarge" style="font-size: 15px; width: 800px;"  rows="15" ${disabled}>${fieldTransform.constructArg!''}</textarea>
                <BR>
                <input class="btn btn-primary" type="button" value="json校验" onclick="formatJson('transformConstructArg')" />
                <div >
                    <span class="header-unit"><font color="red" id = 'showDesc'></font> </span>
                </div>
			</div>
		</div>

		<input type="hidden" name="version" value="${fieldTransform.version!''}"/>
		<div class="form-actions">
			<input id="btnCancel" class="btn" type="button" value="返 回" onclick="goback()" />&nbsp;
			<#if edit>
				<input id="btnReset" class="btn btn-primary" type="reset" value="重 置" onclick="resetForm()"/>&nbsp;
				<input id="btnSubmit" class="btn btn-primary" type="submit" value="保 存" />&nbsp;
			</#if>
		</div>
	</form>
</body>
</html>